﻿@page "/menu"
@using daonaite.Model.system.menu
@using daonaite.ui.Api
@using daonaite.ui.Util
@inject HttpHelp http
@inject RulesHelp rule
@inject apiMenu menuurl
@inject IMessageService message

<Card>
    <Form Model="@queryform" OnFinish="queryFinish" Layout="@FormLayout.Inline">
        <FormItem Label="菜单名称">
            <Input @bind-Value="@context.menuName"></Input>
        </FormItem>
        <FormItem>
            <Button Type="@ButtonType.Primary" HtmlType="submit">搜索</Button>
        </FormItem>
    </Form>
</Card>
<Card>
    <Button OnClick="add">新增</Button>
    <Button Disabled="selects.Count() !=1" OnClick="async e=>{await update(selects.First().id);}">编辑</Button>
    <Popconfirm Title="确认删除数据?" OnConfirm="async e=>{ await pldelete(selects);}">
        <Button Disabled="selects.Count() ==0">删除</Button>
    </Popconfirm>
    <Button>导入</Button>
    <Button>导出</Button>
</Card>
<Card>
    <Table DataSource="@treemenu" @bind-SelectedRows="@selects" TreeChildren="item=>item.child" PaginationPosition="null">
        <Selection></Selection>
        <PropertyColumn Property="c=>c.id" Title="序号"></PropertyColumn>
        <PropertyColumn Property="c=>c.menuName" Title="菜单名称"></PropertyColumn>
        <PropertyColumn Property="c=>c.path" Title="路由"></PropertyColumn>
        <PropertyColumn Property="c=>c.icon" Title="图标">
            <Icon Type="@context.icon"></Icon>
        </PropertyColumn>
        <PropertyColumn Property="c=>c.order" Title="排序"></PropertyColumn>
        <PropertyColumn Property="c=>c.ableFlag" Title="启用状态">
            @if (context.ableFlag == "Y")
            {
                <Tag Color="success" OnClick="async e=>{ await update_ableflag(context);}">启用</Tag>
            }
            else
            {
                <Tag Color="error" OnClick="async e=>{ await update_ableflag(context);}">停用</Tag>
            }
        </PropertyColumn>
        <ActionColumn Title="操作">
            <Button Type="@ButtonType.Text" OnClick="async e=>{await update(context.id);}">编辑</Button>

            <Popconfirm Title="确认删除本条数据?" OnConfirm="async e=>{ await delete(context.id); }">
                <Button Type="@ButtonType.Text">删除</Button>
            </Popconfirm>
            <Button Type="@ButtonType.Text" OnClick="async e=>{ await tablebtn(context.id); }">按钮</Button>
        </ActionColumn>
    </Table>
</Card>
<Modal Title="@menumodal.title" Visible="@menumodal.open" OnCancel="menumodal.cancel" Footer="null">
    <Form Model="@menumodal.menu" LabelColSpan="4" WrapperColSpan="20" OnFinish="menuok" ValidateMode="@FormValidateMode.Rules">
        <FormItem Label="上级菜单">
            <TreeSelect DataSource="@treemenu" ChildrenExpression="node=>node.DataItem.child"
                        TitleExpression="node=>node.DataItem.menuName"
                        KeyExpression="node=>node.DataItem.id.ToString()" @bind-Value="@sfid"></TreeSelect>
        </FormItem>
        <FormItem Label="菜单名称" Rules="@rule.required("请输入菜单名称")">
            <Input @bind-Value="@context.menuName"></Input>
        </FormItem>
        <FormItem Label="路由" >
            <Input @bind-Value="@context.path"></Input>
        </FormItem>
        <FormItem Label="图标">
            <Input @bind-Value="@context.icon"></Input>
        </FormItem>
        <FormItem Label="排序">
            <Input @bind-Value="@context.order"></Input>
        </FormItem>
        <FormItem WrapperColOffset="4">
            <Button Type="@ButtonType.Primary" HtmlType="submit">提交</Button>
            <Button OnClick="menumodal.cancel">取消</Button>
        </FormItem>
    </Form>
</Modal>
<Modal Title="@btnmodal.title" Visible="@btnmodal.open" OnCancel="btnmodal.cancel" Footer="null" Width="800">
    <Form Model="@btnmodal.menu" Layout="@FormLayout.Inline" OnFinish="btnfinish" ValidateMode="@FormValidateMode.Rules">
        <FormItem Label="按钮名"  Rules="@rule.required("请输入按钮名")">
            <Input @bind-Value="@context.menuName"></Input>
        </FormItem>
        <FormItem Label="标识" Rules="@rule.required("请输入标识")">
            <Input @bind-Value="@context.path"></Input>
        </FormItem>
        <FormItem>
            <Button Type="@ButtonType.Primary" HtmlType="submit">添加</Button>
            <Button OnClick="onkeyadd">一键生成</Button>
        </FormItem>
    </Form>
    <Divider />
    <Table DataSource="@listbtn">
        <PropertyColumn Property="c=>c.id" Title="序号"></PropertyColumn>
        <PropertyColumn Property="c=>c.menuName" Title="按钮"></PropertyColumn>
        <PropertyColumn Property="c=>c.path" Title="标识"></PropertyColumn>
        <ActionColumn Title="操作">
            <Popconfirm Title="确认删除本条数据?" OnConfirm="async e=>{ await delete_btn(context.id); }">
                <Button Type="@ButtonType.Text">删除</Button>
            </Popconfirm>
        </ActionColumn>
    </Table>
</Modal>


@code {
    Dto_menu queryform = new Dto_menu();
    List<sys_menu> treemenu = new List<sys_menu>();
    List<sys_menu> listbtn = new List<sys_menu>();

    private string sfid = "";

    private IEnumerable<sys_menu> selects = new List<sys_menu>();

    private class Modals
    {
        public bool open { get; set; } = false;
        public sys_menu menu { get; set; } = new sys_menu();
        public string title { get; set; } = "";
        public int fid { get; set; }

        public void cancel()
        {
            open = false;
        }
    }

    Modals menumodal = new Modals();
    Modals btnmodal = new Modals();


    protected override async Task OnInitializedAsync()
    {
        queryform.pageSize = 10;
        queryform.pageIndex = 1;
        await bindlist();
        await base.OnInitializedAsync();
    }


    private async Task queryFinish()
    {
        queryform.pageSize = 10;
        queryform.pageIndex = 1;
        await bindlist();
    }

    private async Task bindlist()
    {
        var result = await http.posttAsync<List<sys_menu>?, Dto_menu>(menuurl.Gettreemenu, queryform);
        if (result!.Code == 200)
        {
            treemenu = result.Data;
        }
    }

    private async Task update_ableflag(sys_menu data)
    {
        if (data.ableFlag == "Y")
        {
            data.ableFlag = "N";
        }
        else
        {
            data.ableFlag = "Y";
        }
        var result = await http.posttAsync<int?, sys_menu>(menuurl.update, data);
        if (result!.Code != 200)
        {
            if (data.ableFlag == "Y")
            {
                data.ableFlag = "N";
            }
            else
            {
                data.ableFlag = "Y";
            }
        }
    }

    private void add()
    {
        menumodal.open = true;
        menumodal.title = "添加菜单";
        menumodal.menu = new sys_menu();
        sfid = "";
    }

    private async Task update(int id)
    {
        var result = await http.getAsync<sys_menu?>($"{menuurl.get}?id={id}");
        if (result.Code == 200)
        {
            menumodal.open = true;
            menumodal.title = "编辑菜单";
            menumodal.menu = result.Data;
            sfid = result.Data.fid.ToString();
        }
    }

    public async Task menuok()
    {
        if(sfid !="")
        {
            menumodal.menu.fid = int.Parse(sfid);
        }
        
        if (menumodal.menu.id > 0) //编辑
        {
            var result = await http.posttAsync<int?, sys_menu>(menuurl.update, menumodal.menu);
            if (result.Code == 200)
            {
                message.Success("修改成功");
                await queryFinish();
                menumodal.open = false;
            }
        }
        else //新增
        {
            var result = await http.posttAsync<int?, sys_menu>(menuurl.add, menumodal.menu);
            if (result.Code == 200)
            {
                message.Success("添加成功");
                await queryFinish();
                menumodal.open = false;
            }
        }
    }

    private async Task delete(int id)
    {
        var result = await http.getAsync<int?>($"{menuurl.delete}?id={id}");
        if (result.Code == 200)
        {
            message.Success("删除成功");
            await queryFinish();
        }
    }

    private async Task delete_btn(int id)
    {
        var result = await http.getAsync<int?>($"{menuurl.delete}?id={id}");
        if (result.Code == 200)
        {
            message.Success("删除成功");
            var result1 = await http.getAsync<List<sys_menu>?>($"{menuurl.selectButton}?id={btnmodal.fid}");
            if (result1.Code == 200)
            {
                listbtn = result1.Data;
            }

        }
    }

    private async Task pldelete(IEnumerable<sys_menu> mens)
    {
        foreach (var item in mens)
        {
            await delete(item.id);
        }
        await queryFinish();
    }

    private async Task btnfinish()
    {
        btnmodal.menu.fid = btnmodal.fid;
        btnmodal.menu.type = "B";
        var result = await http.posttAsync<int?, sys_menu>(menuurl.add, btnmodal.menu);
        if (result.Code == 200)
        {
            message.Success("添加成功");
            await getbtnlist();
        }
    }

    private async Task getbtnlist()
    {
        var result1 = await http.getAsync<List<sys_menu>?>($"{menuurl.selectButton}?id={btnmodal.fid}");
        if (result1.Code == 200)
        {
            listbtn = result1.Data;
        }
    }

    public async Task tablebtn(int id)
    {
        btnmodal.title = "按钮设置";
        btnmodal.open = true;
        btnmodal.menu = new sys_menu();
        btnmodal.fid = id;

        await getbtnlist();

    }

    private async Task onkeyadd()
    {

        var result = await http.getAsync<int?>($"{menuurl.onkeyadd}?id={btnmodal.fid}");
        if (result.Code == 200)
        {
            await getbtnlist();
        }
    }


}
